package cn.tedu.overhaul.management.service.impl;

import cn.tedu.overhaul.management.config.IsDateInFormat;
import cn.tedu.overhaul.management.dto.persist.mapper.OverhaulMapper;
import cn.tedu.overhaul.management.dto.persist.mapper.OverhaulRentMapper;
import cn.tedu.overhaul.management.dto.persist.mapper.OverhaulToolMapper;
import cn.tedu.overhaul.management.dto.persist.repository.IOverhaulRentRepository;
import cn.tedu.overhaul.management.dto.persist.repository.IOverhaulRepository;
import cn.tedu.overhaul.management.dto.persist.repository.IOverhaulToolRepository;
import cn.tedu.overhaul.management.pojo.dto.*;
import cn.tedu.overhaul.management.pojo.entity.OverhaulRent;
import cn.tedu.overhaul.management.pojo.vo.*;
import cn.tedu.overhaul.management.service.IOverhaulRentService;
import cn.tedu.overhaul.management.service.IOverhaulService;
import cn.tedu.overhaul.management.service.IOverhaulToolService;
import cn.tedu.rtms.commons.ex.ServiceException;
import cn.tedu.rtms.commons.pojo.vo.PageData;
import cn.tedu.rtms.commons.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class OverhaulToolServiceImpl implements IOverhaulToolService {
    @Value("${overhaul.crud.default-query-page-size}")
    private Integer defaultQueryPageSize;
    @Autowired
    OverhaulMapper overhaulMapper;

    @Autowired
    IOverhaulToolRepository iOverhaulToolRepository;

    @Autowired
    OverhaulToolMapper overhaulToolMapper;

    @Override
    public String addNew(ToolAddNewDTO toolAddNewDTO) {

        log.debug("开始处理[添加工具新增],参数:{}",toolAddNewDTO);

        List<String> numberAll=iOverhaulToolRepository.selectByToolNumber();
        for (String s : numberAll) {
            if(toolAddNewDTO.getNumber().equals(s)){
                log.debug("您新增的工具已经存在,{}",toolAddNewDTO.getNumber());
            }
        }

        iOverhaulToolRepository.insertTool(toolAddNewDTO);
        log.debug("执行处理[添加工具新增]成功,参数:{}",toolAddNewDTO);
        return "200";
    }
    @Override
    public PageData<ToolVO> selectRent(Integer pageNum) {
        log.debug("开始处理【借用记录表】的业务，页码：{}", pageNum);
        PageData<ToolVO> pageData=iOverhaulToolRepository.selectRent(pageNum,defaultQueryPageSize);
        return pageData;
    }

    @Override
    public PageData<ToolVO> selectRent(Integer pageNum, Integer pageSize) {
        log.debug("开始处理【借用记录表】的业务，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageData<ToolVO> pageData = iOverhaulToolRepository.selectRent(pageNum, pageSize);
        return pageData;
    }
/*    @Override
    public int updateEndTime(Long id) {
        log.debug("开始处理【归还工具】的业务，参数id：{}", id);
        return iOverhaulRentRepository.updateEndTime(id);
    }*/

    @Override
    public void delete(Long id) {
        log.debug("开始处理【根据ID删除工具记录】的业务，参数：{}", id);
        // 调用Mapper对象的getStandardById()执行查询
        ToolVO queryResult=overhaulToolMapper.selectById(id);
        // 判断查询结果是否为null
        if (queryResult == null) {
            String message = "删除工具记录失败，尝试删除的工具记录数据不存在！";
            log.warn(message);
        }
        // 调用Mapper对象的deleteById()方法执行删除
        log.debug("即将执行删除，参数：{}", id);
        int deleteTool=overhaulToolMapper.deleteById(id);
        log.debug("开始处理【根据ID删除工具记录】的业务完成，反回值：{}", deleteTool);
    }




    @Override
    public ToolAllVO getStandardByIdAll(Long id) {
        log.debug("开始处理【根据ID修改面板工具记录】的业务，参数：{}", id);

        // 调用Mapper对象的getStandardById()执行查询
        ToolAllVO queryResult=iOverhaulToolRepository.getStandardByIdAll(id);

        if(queryResult==null){
            //是:抛出异常
            String message ="根据ID修改面板查询工具记录失败，根据ID修改面板查询检修记录不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        if(queryResult.getModificationTime()==null){
            queryResult.setModificationTime("未修改");
        }
        if(queryResult.getModificationFrequency()==null){
            queryResult.setModificationFrequency(0);
        }
        return queryResult;

    }


    @Override
    public int updateInfoById(Long id , ToolUpdateDTO toolUpdateDTO) {
        log.debug("开始处理【修改工具记录】的业务，参数：{},{}", id,toolUpdateDTO);
        ToolAllVO queryResult= iOverhaulToolRepository.getStandardByIdAll(id);
        if(queryResult==null){
            //是:抛出异常
            String message ="根据ID修改工具记录失败，根据ID修改工具记录不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        ToolAllDTO toolAllDTO=new ToolAllDTO();
        toolAllDTO.setId(id);
        toolAllDTO.setName(toolUpdateDTO.getName());
        toolAllDTO.setNumber(toolUpdateDTO.getNumber());
        toolAllDTO.setProduceDate(toolUpdateDTO.getProduceDate());
        toolAllDTO.setQualityGuaranteePeriod(toolUpdateDTO.getQualityGuaranteePeriod());
        toolAllDTO.setStatus(toolUpdateDTO.getStatus());
        toolAllDTO.setUrl(toolUpdateDTO.getUrl());
        toolAllDTO.setMethod(toolUpdateDTO.getMethod());
        toolAllDTO.setModificationTime(LocalDateTime.now());
        int frequency=0 ;
        if (queryResult.getModificationFrequency()!=null){
            frequency=queryResult.getModificationFrequency();
        }
        frequency++;
        log.debug("参数frequency:{}",frequency);
        toolAllDTO.setModificationFrequency(frequency);
        return iOverhaulToolRepository.update(toolAllDTO);
    }
}
